<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as  echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "../mixins/resize";

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "260px",
    },
    chartdata: {
      type: Array,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      var that = this;
      var data_name = [];
      var data_value = [];
      for (var i = 0; i < that.chartdata.length; i++) {
        data_name[i] = that.chartdata[i].departmentname;
        data_value[i] = that.chartdata[i].ccount;
      }
      this.initChart(data_name, data_value);
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart(data_name, data_value) {
      this.chart = echarts.init(this.$el, "macarons");
      this.chart.setOption({
        title: {
          text: "项目申请统计",
          left: "left",
          padding: [
            10, // 上
            10, // 右
            10, // 下
            10, // 左
          ],
          textStyle: { color: "#606266", fontSize: "14" },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        grid: {
          top: 60,
          left: "2%",
          right: "2%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: data_name,
            axisTick: {
              alignWithLabel: true,
            },
            axisLabel: {
              interval: 0,
              rotate: 38,
              textStyle: {  //数值样式
                color: '#606266',   //字体颜色
              }
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            axisTick: {
              show: false,
            },
            axisLabel: {
              textStyle: {  //数值样式
                color: '#606266',   //字体颜色
              }
            },
          },
        ],
        series: [
          {
            name: "数量",
            type: "bar",
            stack: "vistors",
            barWidth: "18px",
            data: data_value,
            itemStyle: {
              normal: {
                label: {
                  show: true, //开启显示数值
                  position: "top", //数值在上方显示
                  textStyle: {
                    //数值样式
                    color: "#F0A5BE", //字体颜色
                    fontSize: 14, //字体大小
                  },
                },
                color: function (params) {
                  var colorList = ["#F0A5BE"];
                  return colorList[0];
                },
              },
            },
          },
        ],
      });
    },
  },
};
</script>
